<template>
  <div class="register-bg">
    <div class="layui-fulid" id="house-register">
      <div class="layui-form">
        <p>用户注册</p>
        <el-form :model="memberUser" status-icon :rules="rules" ref="memberUser" label-position="top" label-width="100px">
          <el-form-item prop="mobile">
            <el-input type="text"  v-model="memberUser.mobile" placeholder="请输入手机号码" autocomplete="off">
              <i slot="prefix" class="el-input__icon el-icon-user"></i>
            </el-input>
          </el-form-item>

          <el-form-item prop="password">
            <el-input type="password" v-model="memberUser.password"  placeholder="请输入密码"  autocomplete="true">
              <i slot="prefix" class="el-input__icon el-icon-unlock"></i>
            </el-input>
          </el-form-item>

          <el-form-item style="text-align: center">
            <img :src="gzh_ewm" style="width: 35%"/>
            <div style="text-align: center; font-size: 14px; color: #FF5722;">
              关注微信公众号,发送手机号码可获得注册码
            </div>
          </el-form-item>

          <el-form-item prop="registCode">
            <el-input type="text"  v-model="memberUser.registCode" placeholder="请输入微信注册码" autocomplete="off">
              <i slot="prefix" class="el-input__icon el-icon-message"></i>
            </el-input>
          </el-form-item>

          <el-form-item prop="imageCode">
            <el-input type="text" v-model="memberUser.imageCode" placeholder="请输入验证码结果" style="width: 60%;float: right"  autocomplete="true">
            </el-input>
            <img :src="imgUrl" @click="getCaptcha" :data_code="verCode" style="width: 40%;float: right" />
          </el-form-item>

          <el-form-item>
            <el-button type="primary" @click="register('memberUser')">注 册</el-button>
          </el-form-item>

        </el-form>

      </div>
    </div>
  </div>
</template>

<script>
  import '~/assets/static/css/register.css'
  import userApi from '@/api/user'
  import { setUser } from '@/utils/auth'
  export default {
    data(){
      //自定义校验验证码
      var checkCode = (rule,value,callback)=>{
        if(!value){
          return callback(new Error('请输入验证码结果'));
        }
        if(this.verCode !== value){
          return callback(new Error('验证码结果不正确'));
        }
        callback();
      };
      var checkMobile = (rule,value,callback)=>{
        if(!value){
          return callback(new Error('请输入手机号码'));
        }
        if(!(/^1[3-9]\d{9}$/).test(value)){
          return callback(new Error('请输入正确的手机号码'));
        }
        callback();

      };
      return {
        memberUser: {
          mobile: '',
          password:'',
          registCode:'',
          imageCode:'',
        },
        rules: {
          mobile: [
            { validator:checkMobile, trigger: 'blur' }
          ],
          password: [
            { required: true, message: '请输入密码', trigger: 'blur' },
            { min: 6,  message: '密码长度至少6位', trigger: 'blur' }
          ],
          weixinCode:[
            { required: true, message: '请输入微信注册码', trigger: 'blur' }
          ],
          imageCode: [
            { validator: checkCode, trigger: 'blur' }
          ],

        },
        imgUrl:"",
        verCode:"",
        gzh_ewm:'/gzh_ewm.jpg',
      }
    },
    created() {
      this.getCaptcha();
    },
    methods: {
      getCaptcha() {
        userApi.getRegisterCaptcha().then(res => {
          if (res.data.flag) {
            this.imgUrl = res.data.data.imgUrl;
            this.verCode = res.data.data.verCode;
          }
        })
      },
      register(memberUser){
        this.$refs[memberUser].validate((valid) => {
          if (valid) {
            userApi.register( this.memberUser).then(res => {
              if(res.data.flag){
                this.$message({
                  message: '注册成功',
                  type: 'success'
                })
                //保存用户信息
                this.memberUser={}
              }else{
                this.$message({
                  message: res.data.msg,
                  type: 'error'
                })
              }
            })
          } else {
            return false;
          }
        });
      }
    }
  }
</script>

<style scoped>

</style>
